/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package z.h.w.jar.kit.clazz;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Collection;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;
import z.h.w.jar.data.time.Time;

/**
 *
 * @author EnzoZhong
 */
public class XML {

      private Properties properties = new Properties ();
      private File file;

      public XML ( String file ) throws IOException {
            this ( new File ( file.endsWith ( ".xml" ) ? file : file + ".xml" ) );
      }

      private XML ( File file ) throws IOException {
            properties = new Properties ();
            this.file = file;
            if (  ! file.exists () ) {
                  file.createNewFile ();
                  properties.storeToXML ( new FileOutputStream ( file ) , Time.born ().toString () , "utf8" );
            }
      }

      public Map<Object , Object> read () {
            try {
                  properties.loadFromXML ( new FileInputStream ( file ) );
            } catch ( IOException ex ) {
                  System.err.println ( ex );
            }
            return properties;
      }

      public XML save () {
            return save ( new HashMap<> ( 1 ) );
      }

      public XML save ( Map<Object , Object> map ) {
            try {
                  properties.putAll ( map );
                  properties.storeToXML ( new FileOutputStream ( file ) , Time.born ().toString () , "utf8" );
            } catch ( IOException ex ) {
                  ex.printStackTrace ();
            }
            return this;
      }

      public synchronized int size () {
            return properties.size ();
      }

      public synchronized boolean isEmpty () {
            return properties.isEmpty ();
      }

      public synchronized Enumeration<Object> keys () {
            return properties.keys ();
      }

      public synchronized Enumeration<Object> elements () {
            return properties.elements ();
      }

      public synchronized boolean contains ( Object value ) {
            return properties.contains ( value );
      }

      public boolean containsValue ( Object value ) {
            return properties.containsValue ( value );
      }

      public synchronized boolean containsKey ( Object key ) {
            return properties.containsKey ( key );
      }

      public synchronized Object get ( Object key ) {
            return properties.get ( key );
      }

      public synchronized Object put ( Object key , Object value ) {
            return properties.put ( key , value );
      }

      public synchronized Object remove ( Object key ) {
            return properties.remove ( key );
      }

      public synchronized void putAll ( Map<? extends Object , ? extends Object> t ) {
            properties.putAll ( t );
      }

      public synchronized void clear () {
            properties.clear ();
      }

      @Override
      public synchronized Object clone () {
            return properties.clone ();
      }

      @Override
      public synchronized String toString () {
            return properties.toString ();
      }

      public Set<Object> keySet () {
            return properties.keySet ();
      }

      public Set<Entry<Object , Object>> entrySet () {
            return properties.entrySet ();
      }

      public Collection<Object> values () {
            return properties.values ();
      }

}
